package org.eureka.manager.web;

import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Objects;

/**
 * @author weizhao.dong
 * @Date: 2020/9/16 上午10:05
 */
@RequestMapping("/manage")
public class EurekaManageController {
    @Autowired
    private ApplicationInfoManager applicationInfoManager;

    @RequestMapping("/online")
    @ResponseBody
    public Object online() {
        return changeInstanceStatus(InstanceInfo.InstanceStatus.UP);
    }

    @RequestMapping("/offline")
    @ResponseBody
    public Object offline() {
        return changeInstanceStatus(InstanceInfo.InstanceStatus.DOWN);
    }


    private boolean changeInstanceStatus(InstanceInfo.InstanceStatus status) {
        if (Objects.nonNull(applicationInfoManager) && Objects.nonNull(status)) {
            applicationInfoManager.setInstanceStatus(status);
            return true;
        }
        return false;
    }
}
